import com.example.demo8.Employee;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/employee")
public class EmployeeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      String action=req.getParameter("action");
      if("sign_in".equals(action)){
          signIn(req,resp);
      }
    }
    protected void signIn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name=req.getParameter("name");
        String password=req.getParameter("password");
        String sqlTest="select * from oa_employee.employee where name=? and PWD=?";
        Connection  connection=DB.getConnection();
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            preparedStatement=connection.prepareStatement(sqlTest);
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,password);
            resultSet=preparedStatement.executeQuery();
            resultSet.next();
            if(resultSet!=null){
                HttpSession session = req.getSession();
                req.getSession().setAttribute("name", name);
                req.getSession().setAttribute("employees", getAllEmployees());
                resp.sendRedirect("admin.jsp");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DB.close(resultSet, preparedStatement, connection);
        }


    }
    private ArrayList<Employee> getAllEmployees() {
        Connection connection = DB.getConnection();
        String sqlList = "select * from oa_employee.employee ";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            preparedStatement = connection.prepareStatement(sqlList);
            resultSet = preparedStatement.executeQuery();
            ArrayList<Employee> employees = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String password=resultSet.getString("PWD");
                int age = resultSet.getInt("AGE");
                String sex = resultSet.getString("Sex");
                Employee employee = new Employee(id, name,password, age, sex);
                employees.add(employee);
            }
            return employees;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DB.close(resultSet, preparedStatement, connection);
        }
    }
}
